{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.contrib import rnn\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   FTR  HTGS  ATGS  HTGC  ATGC  HTP  ATP  HM1  HM2  HM3   ...    ATWinStreak5  \\\n",
      "0    0   0.0   0.0   0.0   0.0  0.0  0.0    2    2    2   ...               0   \n",
      "1    1   0.0   0.0   0.0   0.0  0.0  0.0    2    2    2   ...               0   \n",
      "2    1   0.0   0.0   0.0   0.0  0.0  0.0    2    2    2   ...               0   \n",
      "3    1   0.0   0.0   0.0   0.0  0.0  0.0    2    2    2   ...               0   \n",
      "4    1   0.0   0.0   0.0   0.0  0.0  0.0    2    2    2   ...               0   \n",
      "\n",
      "   ATLossStreak3  ATLossStreak5  HTGD  ATGD  DiffPts  DiffFormPts  DiffLP  \\\n",
      "0              0              0   0.0   0.0      0.0          0.0   -15.0   \n",
      "1              0              0   0.0   0.0      0.0          0.0    10.0   \n",
      "2              0              0   0.0   0.0      0.0          0.0   -16.0   \n",
      "3              0              0   0.0   0.0      0.0          0.0    -1.0   \n",
      "4              0              0   0.0   0.0      0.0          0.0     5.0   \n",
      "\n",
      "   final1  final2  \n",
      "0     1.0     0.0  \n",
      "1     0.0     1.0  \n",
      "2     0.0     1.0  \n",
      "3     0.0     1.0  \n",
      "4     0.0     1.0  \n",
      "\n",
      "[5 rows x 30 columns]\n",
      "(1860, 30)\n"
     ]
    }
   ],
   "source": [
    "data=pd.read_csv(\"allAtt_onehot_large_train.csv\")\n",
    "dataT=pd.read_csv(\"allAtt_onehot_large_test.csv\")\n",
    "print(data.head())\n",
    "print(data.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "hm_epochs=10\n",
    "n_classes = 2\n",
    "batch_size = 1\n",
    "chunk_size=27\n",
    "n_chunks=1\n",
    "rnn_size=64\n",
    "\n",
    "\n",
    "x = tf.placeholder('float', [None, n_chunks,chunk_size])\n",
    "y = tf.placeholder('float')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recurrent_neural_model(x):\n",
    "    layer = {'weights':tf.Variable(tf.random_normal([rnn_size,n_classes])),\n",
    "            'biases':tf.Variable(tf.random_normal([n_classes]))}\n",
    "    \n",
    "    x=tf.transpose(x,[1,0,2])\n",
    "    print(\"transpose\",x)\n",
    "    x=tf.reshape(x,[-1,chunk_size])\n",
    "    print(\"reshape\",x)\n",
    "    x=tf.split(x,n_chunks)\n",
    "    print(\"split\",x)\n",
    "    lstm_cell = rnn.BasicLSTMCell(rnn_size)\n",
    "    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)\n",
    "    \n",
    "\n",
    "\n",
    "    output = tf.matmul(outputs[-1],layer['weights']) + layer['biases']\n",
    "\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_neural_network(x):\n",
    "    prediction = recurrent_neural_model(x)\n",
    "    cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y) )\n",
    "    optimizer = tf.train.AdamOptimizer().minimize(cost)\n",
    "    \n",
    "    with tf.Session() as sess:\n",
    "        sess.run(tf.global_variables_initializer())\n",
    "        hm_epochs=10\n",
    "        for epoch in range(hm_epochs):\n",
    "            epoch_loss = 0\n",
    "            for i in range(0,data.shape[0],batch_size):\n",
    "                epoch_x, epoch_y = data.iloc[i:i+batch_size,1:28].values,data.iloc[i:i+batch_size,28:].values\n",
    "                epoch_x=epoch_x.reshape((batch_size,n_chunks,chunk_size))\n",
    "                _, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})\n",
    "                epoch_loss += c\n",
    "\n",
    "            print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)\n",
    "\n",
    "        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))\n",
    "\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))\n",
    "        print('Accuracy Train:',accuracy.eval({x:data.iloc[:,1:28].values.reshape((-1,n_chunks,chunk_size)), \n",
    "                                               y:data.iloc[:,28:].values}))\n",
    "        print('Accuracy Test:',accuracy.eval({x:dataT.iloc[:,1:28].values.reshape((-1,n_chunks,chunk_size)), \n",
    "                                              y:dataT.iloc[:,28:].values}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "transpose Tensor(\"transpose:0\", shape=(1, ?, 27), dtype=float32)\n",
      "reshape Tensor(\"Reshape:0\", shape=(?, 27), dtype=float32)\n",
      "split [<tf.Tensor 'split:0' shape=(?, 27) dtype=float32>]\n",
      "WARNING:tensorflow:From <ipython-input-6-47c7d36f583f>:3: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n",
      "Epoch 0 completed out of 10 loss: 1216.3304373975843\n",
      "Epoch 1 completed out of 10 loss: 1075.46176257357\n",
      "Epoch 2 completed out of 10 loss: 980.8723999504\n",
      "Epoch 3 completed out of 10 loss: 864.5714094936848\n",
      "Epoch 4 completed out of 10 loss: 738.5878906969447\n",
      "Epoch 5 completed out of 10 loss: 614.039942086878\n",
      "Epoch 6 completed out of 10 loss: 492.0997193742478\n",
      "Epoch 7 completed out of 10 loss: 377.4936973380577\n",
      "Epoch 8 completed out of 10 loss: 277.3404195612128\n",
      "Epoch 9 completed out of 10 loss: 196.47607771527558\n",
      "Accuracy Train: 0.97849464\n",
      "Accuracy Test: 0.80875\n"
     ]
    }
   ],
   "source": [
    "train_neural_network(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
